package com.keon.jianbao.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.enu.PasswordActivtyType;
import com.keon.jianbao.util.StringUtils;

import org.apache.http.Header;
import org.json.JSONException;

import butterknife.Bind;

/**
 * Created by keon on 2015/9/6.
 */
public class RegisterActivity2 extends BaseActivity {

    @Bind(R.id.register_old)
    EditText etOldPassword;
    @Bind(R.id.register_old_layout)
    LinearLayout llOldPassword;
    @Bind(R.id.register_password)
    EditText etPassword;
    @Bind(R.id.register_password_confirm)
    EditText etPasswordConfirm;
    @Bind(R.id.register_submit)
    Button bSubmit;

    int type;

    SimpleHandler registerHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    RegisterActivity2.showToast(R.string.register_success);
                    AppManager.getAppManager().finishActivity(RegisterActivity.class);
                    AppManager.getAppManager().finishActivity(RegisterActivity2.class);
                    RegisterActivity2.this.jump(LoginActivity.class);
                } else {
//                    RegisterActivity2.showToast(result.getTips());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    SimpleHandler editPasswordHandler = new SimpleHandler(this) {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    RegisterActivity2.showToast(R.string.edit_password_success);
                    AppManager.getAppManager().finishActivity(RegisterActivity.class);
                    AppManager.getAppManager().finishActivity(RegisterActivity2.class);
                    RegisterActivity2.this.jump(MainActivity.class);
                } else {
//                    RegisterActivity2.showToast(result.getTips());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register2;
    }

    @Override
    protected int getToolBarTitle() {
        if (type == PasswordActivtyType.REGISTER.getValue()) {
            return R.string.register;
        } else if (type == PasswordActivtyType.FORGET.getValue()) {
            return R.string.forget_password;
        } else if (type == PasswordActivtyType.EDIT.getValue()) {
            return R.string.edit_password;
        }
        return R.string.register;
    }

    @Override
    public void initView() {
        bSubmit.setOnClickListener(this);
        setRightTitle();

        Bundle bundle = getIntent().getExtras();
        type = bundle.getInt("type");

        if (type != PasswordActivtyType.REGISTER.getValue()) {
            bSubmit.setText("确定");
        }

        if (type == PasswordActivtyType.EDIT.getValue()) {
            llOldPassword.setVisibility(View.VISIBLE);
        } else {
            llOldPassword.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {
        registerHandler.setContext(this);
        editPasswordHandler.setContext(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.register_submit:
                if (check()) {
                    if (type == PasswordActivtyType.REGISTER.getValue()) {
                        Bundle bundle = getIntent().getExtras();
                        Api.register(bundle.getString("mobile"), etPassword.getText().toString(), registerHandler);
                    } else if (type == PasswordActivtyType.EDIT.getValue()) {
                        if (checkOld()) {
                            Api.alter_password(AppContext.getInstance().getProperty("user.code"), etOldPassword.getText().toString(),
                                    etPassword.getText().toString(), editPasswordHandler);
                        }

                    } else if (type == PasswordActivtyType.FORGET.getValue()) {
                        showToast("暂未有此功能");
                    }

                }
                break;
        }
    }

    private void setRightTitle() {
        TextView v = new TextView(this);
        v.setTextColor(getResources().getColor(R.color.white));
        v.setPadding(0, 0, 20, 0);
        v.setText("(2/2)");
        setRightView(v);
    }

    private boolean checkOld() {
        String op = etOldPassword.getText().toString();
        if (StringUtils.isEmpty(op)) {
            showToast(R.string.old_password_error_null);
            return false;
        }
        return true;
    }

    private boolean check() {
        String p = etPassword.getText().toString();
        String pc = etPasswordConfirm.getText().toString();

        if (StringUtils.isEmpty(p)) {
            showToast(R.string.register_password_error_null);
            return false;
        }
        if (StringUtils.isEmpty(pc)) {
            showToast(R.string.register_password_confirm_error_null);
            return false;
        }
        if (!p.equals(pc)) {
            showToast(R.string.register_password_error_no_same);
            return false;
        }
        return true;
    }
}
